<!-- 详细记录信息 -->
<template>
  <div class="pimrecord2">
    <van-form>
      <van-field
        v-model="companyName"
        name="公司名称"
        label="公司名称："
        placeholder="公司名称"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        readonly
        clickable
        name="picker"
        :value="nature"
        label="公司性质："
        placeholder="请选择公司性质"
        @click="showPicker = true"
      />
      <van-popup v-model="showPicker" position="bottom">
        <van-picker
          show-toolbar
          :columns="columns"
          @confirm="onConfirm"
          @cancel="showPicker = false"
        />
      </van-popup>
      <!-- <van-field
        readonly
        clickable
        name="picker1"
        :value="industry"
        label="所属行业："
        placeholder="请选择所属行业"
        @click="showPicker1 = true"
      />
      <van-popup v-model="showPicker1" position="bottom">
        <van-picker
          show-toolbar
          :columns="columns1"
          @confirm="onConfirm1"
          @cancel="showPicker1 = false"
        />
      </van-popup> -->

      <!-- <van-field name="uploader" label="营业执照：">
        <template #input>
          <van-uploader v-model="files" :after-read="afterRead" :max-count="2" />
        </template>
      </van-field> -->

      <div style="margin: 16px;">
        <van-button round block type="info" @click="upgrade" v-if="info">升级</van-button>
        <van-button round block type="info" @click="onSubmit" v-else :loading="loading">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  name: "PimRecord2",
  data() {
    return {
      info:'',//是否为公司企业 
      loading:false,
      companyName: "", //公司名称
      files: [], //获取的数据
      // license: "", //base64 营业执照存储

      show: false,

      showPicker: false,
      columns: [],
      nature_id: "", //公司性质id
      nature: "", //公司性质

      // showPicker1: false,
      // industry: "", //行业
      // columns1: [],
      // industry_id: "", // 行业ID存储

      avatar: "",
      rules: {
        companyName: [
          { required: true, message: "请输入公司名称", trigger: "blur" },
          { min: 2, message: "请输入正确的公司名称", trigger: "blur" }
        ],
        industr: [
          { required: true, message: "请选择所属行业", trigger: "blur" }
        ],
        businessLicense: [
          {
            required: true,
            message: "请上传营业执照",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    //图片上传服务器
    afterRead(file) {
      // 此时可以自行将文件上传至服务器
      console.log(file);
      this.license = file.content;
      console.log(file.content);
    },

    //公司性质选择
    onConfirm(value, index) {
      this.nature = value;
      this.nature_id = this.natured[index].id;
      this.showPicker = false;
      console.log(`当前值：${value}, 当前索引：${index}`, this.nature_id);
    },
    //行业选择
    // onConfirm1(value, index) {
    //   this.industry = value;
    //   this.industry_id = this.industryd[index].id;
    //   this.showPicker1 = false;
    //   console.log(`当前值：${value}, 当前索引：${index} `, this.industry_id);
    // },
    //获取公司信息
    async getInfor() {
      let user_id = window.localStorage.getItem('user_id');
      let status = "company";
      await this.$http({
        method: "post",
        url: "index/web/doSelfInfos",
        data: {
          user_id,
          status
        }
      })
        .then(res => {
          console.log(res, "公司信息");
          if (res.data.State.Code == 200) {
            this.companyName = res.data.data.name;
           
            this.nature_id = res.data.data.nature_id;
            if(this.nature_id==2){
              this.nature ='小规模纳税人'
            }else{
              this.nature = '一般纳税人'
            }
            // this.industry = res.data.data.industry;
            // this.industry_id = res.data.data.industry_id;
            // this.$store.commit("license", res.data.data.license);
            this.files.push({ url: `${this.$store.state.license}` });
            // console.log(21121, this.files);
            this.show = false;
          } else if (res.data.State.Code == 2005) {
            this.info = true;
          }
        })
        .catch(err => {
          console.log("获取公司信息接口出现问题...");
        });
    },

    httpInfor() {
      //获取公司性质
      this.$http({
        method: "post",
        url: "index/common/getNature"
      })
        .then(res => {
          if (res.data.State.Code == 200) {
            this.natured = res.data.data;
            // console.log(res, this.natured, 222222222222);
            for (const item of res.data.data) {
              let columns = `${item.name}`;
              this.columns.push(columns);
            }
            console.log(this.columns);
          }
        })
        .catch(err => {
          console.log(err);
        });
      //获取行业
      // this.$http({
      //   method: "post",
      //   url: "index/common/getIndustry"
      // })
      //   .then(res => {
      //     if (res.data.State.Code == 200) {
      //       this.industryd = res.data.data;
      //       console.log(this.industryd, 434434343);
      //       for (const items of res.data.data) {
      //         let columns1 = `${items.name}`;
      //         this.columns1.push(columns1);
      //       }
      //       console.log(this.columns1);
      //     }
      //     // console.log(res, "行业 ");
      //   })
      //   .catch(err => {
      //     console.log(err);
      //   });
    },

    //升级为企业用户
    upgrade() {
      // console.log(this.license, 12313131);
      if (
        this.companyName &&
        this.nature_id 
      ) {
        this.loading = true
        // 不能为空
        const user_id = window.localStorage.getItem('user_id');
        let param = {
          phone: window.localStorage.getItem('phone'),
          name: this.companyName,
          nature_id: this.nature_id,
        };
        // console.log(user_id);
        this.$http({
          method: "post",
          url: `index/index/userMoveUp`,
          headers: { "X-Requested-With": "XMLHttpRequest" }, // 必须加这个请求头 否则报错：500
          data: {
            param,
            user_id
          }
        })
          .then(res => {
            console.log(res);
            const _data = res.data;
            if (_data.State.Code == 200) {
              this.loading = false
              this.$Notify({
                type: "success",
                message: "已经成功提交了审核..."
              });
              this.info = false
                this.$router.push({ path: "/pim/pimrecord3" });
            }
            if (_data.State.Code == 2003) {
              this.$Notify({
                type: "success",
                message: "此账户已经关联了公司..."
              });
            }
            this.disabled = true;
          })
          .catch(err => {
            console.log(err);
          });
      } else {
        return;
      }
    },
    //提交修改企业用户
    onSubmit() {
      
      if (
        this.companyName &&
        this.nature_id 
      ) {
        this.loading = true
        // 不能为空
        const user_id =  window.localStorage.getItem('user_id');
        // let license = this.$store.state.license; // 老的营业执照
        let status = "companyUpdate"; // 状态码
        let param = {
          name: this.companyName,
          nature_id: this.nature_id, 
        };
        // console.log(user_id);
        this.$http({
          method: "post",
          url: `index/web/doSelfInfos`,
          headers: { "X-Requested-With": "XMLHttpRequest" }, // 必须加这个请求头 否则报错：500
          data: {
            param,
            user_id,
            status
          }
        })
          .then(res => {
            console.log(res, 111111111111);
            if (res.data.State.Code == 200) {
              this.loading = false
              this.$Notify({
                type: "danger",
                message: "已经成功提交了审核..."
              });
              this.$router.push({ path: "/pim/pimrecord3" });
            }
            if (res.data.State.Code == 400) {
              this.$Notify({
                type: "danger",
                message: "请选择要上传的图片..."
              });
            }
            if (res.data.State.Code == 2003) {
              this.$Notify({
                type: "danger",
                message: "此账户已经关联了公司..."
              });
            }
            this.disabled = true;
          })
          .catch(err => {
            console.log(err);
          });
      } else {
        console.log("数据不全");
      }
    }
  },
  watch:{
    // nature_id(oldvalue,newvalue){
    //   console.log(oldvalue,newvalue)
    // }
  },
  created() {
    this.httpInfor();
    
  },
  mounted() {
    this.getInfor();
  }
};
</script>
<style lang='scss' scoped>
.pimrecord2 {
  margin: 0 0.5405rem 1.6216rem 0;
}
</style>